/**
 * Created by IntelliJ IDEA.
 * Time: 22:54
 * @Author: xyb
 * @Version: 2017/7/6
 * To change this template use File | Settings | File Templates.
 */

(function () {

    var $view;
    var $form;
    var $listItem;
    var $input;
    var $mobile;
    var $email;
    var $pw;
    var $surePw;
    var $nickname;
    var $submit;

    function onInit() {

        onInitData();

        onTabSex();

        monitorEdit();

        userRegister();

    }

    function onInitData() {
        $form = $view.find('#form');
        $listItem = $form.find(".list-item");
        $input = $listItem.find("input");
        $mobile = $listItem.find('.mobile');
        $email = $listItem.find('.email');
        $pw = $listItem.find('.pw');
        $surePw = $listItem.find('.sure_pw');
        $nickname = $listItem.find('.nickname');
        $submit = $form.find('.submit');
    }

    function onTabSex() {
        var $sex = $listItem.find('.sex_model');
        var $sexTab = $sex.find('.sex_tab');
        $sexTab.on("click", function () {
            var $this = $(this);
            if ($this.hasClass("selected")) {
                return false;
            } else {
                $this.addClass("selected").siblings().removeClass("selected");
            }
            if ($sexTab.hasClass("selected")) {
                var value = $sexTab.attr("value");
                $sex.find('#sex').val(value);
            }
        });
    }

    /**
     * 监听input输入
     */
    function monitorEdit() {
        $mobile.blur(function () {
            if ($(this).val() != "" && $(this).val() != null) {
                if (!app.validateMobileNum($(this).val())) {
                    return onElValidFailFocus($mobile, "请输入正确的手机号!");
                }
            }
        });
        $email.blur(function () {
            if ($(this).val() != "" && $(this).val() != null) {
                if (!app.validateEmail($(this).val())) {
                    return onElValidFailFocus($email, "请输入正确的邮箱!");
                }
            }
        });
        $pw.blur(function () {
            if ($(this).val() != "" && $(this).val() != null) {
                if ($(this).val().length < 6 || $(this).val().length > 16) {
                    return onElValidFailFocus($pw, "请输入6-16位的密码!");
                }
                if ($surePw.val().trim() != "") {
                    if ($surePw.val().trim() !== $pw.val().trim()) {
                        return onElValidFailFocus($pw, "两次输入的密码不一致!");
                    }
                }
            }
        });
        $surePw.blur(function () {
            if ($pw.val().trim() != "" && $surePw.val().trim() != "") {
                if ($(this).val().trim() !== $pw.val().trim()) {
                    return onElValidFailFocus($surePw, "两次输入的密码不一致!");
                }
            }
        });
    }

    /**
     * input验证失败
     */
    function onElValidFailFocus(el, msg) {
        app.showToast(msg);
        el.focus();
        return false;
    }

    /**
     * 用户注册
     */
    function userRegister() {
        $submit.on("click", function () {
            var sex = $form.find('#sex').val();
            var mobile = $mobile.val();
            var pass = $pw.val();
            var email = $email.val();
            var nickname = $nickname.val();
            // console.log(sex + " ," + mobile + " " + pass + " " + email + " " + nickname)
            if (sex == "" || mobile == "" || pass == "" || email == "" || nickname == "") {
                app.showToast("请完成所有项目填写");
            } else {
                var data = {
                    sex: sex,
                    mobile: mobile,
                    password: pass,
                    nickName: nickname,
                    email: email
                };
                backService.user.onRegister(data, function (json) {
                    app.showToast(json.msg, "3000");
                    if (json.data == 1) {
                        app.gotoPage("/user/login.html");
                    }
                }, function (error) {
                    app.showToast(error.msg);
                });
            }
        });
    }


    window.onViewInit_user_register = function (view) {
        $view = view.element;
        onInit();
    };

})();